
from route_generator.rountforapp import genRouteFig
from tools.common_function import *
import json

def loadCSV(f_name):
    c = open(f_name, 'r', encoding='utf-8').read().strip('\n').split('\n')
    return [r.split(',') for r in c]

spot2code = {}
for spot in loadCSV('./route_generator/spots.csv'):
    spot2code[spot[1]] = spot[0]


# search_for_route?avgpricevalue=1924&rangevalue=0&spotname=庾村
def searchForRoute(request):
    avgpricevalue = request.GET.get('avgpricevalue')
    rangevalue = request.GET.get('rangevalue')
    spotname = request.GET.get('spotname')

    spotvalue = spot2code[spotname]
    img_path = genRouteFig(avgpricevalue, rangevalue, spotvalue)

    return jsonResponse({'img_url': 'static/route_img/{}.png'.format(img_path)})

# search_for_route?avgpricevalue=1924&rangevalue=0&spotvalue=7
def searchForComments(request):
    spotname = request.GET.get('spotname')

    comments = loadCSV('./comment_analysis/{}.csv'.format(spotname))

    return jsonResponse({
        'img_url': 'static/comment_img/{}.png'.format(spotname),
        'comments': [
            {
                'user': c[0],
                'date': c[1],
                'content': c[2],
            }
            for c in comments[1:]
        ]
    })
